//
//  MessageCell.m
//  QoilPublic
//
//  Created by SunChao on 2017/12/11.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "MessageCell.h"

@implementation MessageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (void)setFrame:(CGRect)frame
{
    // 更改x、宽度
    frame.origin.x = 12;
    frame.size.width -= 12 * 2;
    
    // 更改顶部间距、每个cell之间的间距
    frame.origin.y += 10;
    frame.size.height -= 10;
    
    [super setFrame:frame];
}
- (void)setCellWithData:(OrderListModel *)model{
    self.orderNum.text = [NSString stringWithFormat:@"订单编号%@",model.orderId];
    self.orderAddress.text = model.adress;
    NSArray *array = [model.stateTime componentsSeparatedByString:@","];
    NSString *timeString = [NSString stringWithFormat:@"%@",[array lastObject]];
    NSString *time = [self compareCurrentTime:timeString];
    self.timeLabel.text = time;
   

    [self.orderStatusBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.orderStatusBtn.layer.cornerRadius = 5;
    self.orderStatusBtn.layer.masksToBounds = YES;
    [self.orderStatusBtn setBackgroundColor:[UIColor colorWithRed:90/255.0 green:176/255.0 blue:70/255.0 alpha:1.0f]];
    NSArray *stateArr = [model.stateText componentsSeparatedByString:@","];
    NSString *stateString = [stateArr lastObject];
    NSString *state = nil;
    if ([stateString isEqualToString:@"下单"]) {
        state = @"已下单";
        self.orderStatusLabel.text = [NSString stringWithFormat: @"您的订单已接单，正在配送中..."];
    }else if ([stateString isEqualToString:@"配送"]){
        state = @"配送中";
         self.orderStatusLabel.text = [NSString stringWithFormat: @"您的订单已接单，正在配送中..."];
    }else if ([stateString isEqualToString:@"加油"]){
        state = @"加油中";
         self.orderStatusLabel.text = [NSString stringWithFormat: @"您的订单正在加油..."];
    }else if ([stateString isEqualToString:@"完成"]){
        state = @"加油完成";
         self.orderStatusLabel.text = [NSString stringWithFormat: @"您的订单加油完成，待付款..."];
    }else if ([stateString isEqualToString:@"付款"]){
        state = @"已付款";
         self.orderStatusLabel.text = [NSString stringWithFormat: @"您的订单已付款，待评价..."];
    }else if ([stateString isEqualToString:@"评价"]){
        state = @"已评价";
         self.orderStatusLabel.text = [NSString stringWithFormat: @"您的订单已完成..."];
    }else if ([stateString isEqualToString:@"结束"]){
        state = @"已取消";
         self.orderStatusLabel.text = [NSString stringWithFormat: @"您的订单已取消..."];
        [self.orderStatusBtn setBackgroundColor:[UIColor colorWithRed:255/255.0 green:75/255.0 blue:79/255.0 alpha:1.0f]];
    }else if ([stateString isEqualToString:@"拒单"]){
        state = @"已拒单";
         self.orderStatusLabel.text = [NSString stringWithFormat: @"您的订单已被司机拒单..."];
         [self.orderStatusBtn setBackgroundColor:[UIColor colorWithRed:255/255.0 green:75/255.0 blue:79/255.0 alpha:1.0f]];
    }
    
    [self.orderStatusBtn setTitle:state forState:UIControlStateNormal];
}
- (NSString *) compareCurrentTime:(NSString *)str
{
    
    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *timeDate = [dateFormatter dateFromString:str];
    
    //得到与当前时间差
    NSTimeInterval  timeInterval = [timeDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    //标准时间和北京时间差8个小时
    //    timeInterval = timeInterval - 8*60*60;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
}

@end
